<?php

namespace app\studentLayer\controller;

use think\Controller;
use think\Db;

class Course extends Controller {

    public function SelectCourse()
    {


        if($this->request->isPost()){
            $limit = $this->request->post();
            //var_dump($limit);die;
            $SelectCourse = Db::table('s_course')->page($limit['page'],$limit['limit'])->select();
            $count =Db::table('s_course')->count();
            $this->Ajax('','',$SelectCourse,$count);
        }else{

//            if($this->request->isAjax()){
//                $SelectCourse = Db::table('s_course')->select();
//                $this->Ajax('','',$SelectCourse);
//            }
           return $this->fetch('CourseView/SelectCourse');
        }
    }


    public function InsertCourse(){

        if($this->request->isPost()){

            $InsertCourseData = $this->request->post();
//            insert into s_course(`name`) values("小学语文");
            $InsertCourse = Db::table('s_course')->insert($InsertCourseData);
            if($InsertCourse){
                return $this->Ajax('','添加成功');die;
                //return $this->success('添加成功','Course/SelectCourse');
            }else{
                return $this->error('添加失败','Course/InsertCourse');
            }
        }else{
            return $this->fetch('CourseView/InsertCourse');
        }
    }


    public function DeleteCourse(){

        $DeleteCourseData = $this->request->get();

        //var_dump($DeleteCourseData);die;
//        delete from s_course where id=11

        //开启事务，这里之所以不在模板层写，是因为忘记了，在写老师表的时候发现，科目表也需要加上事务，懒的在给，就这样写吧，反正是对的
        Db::startTrans();
        $DeleteCourse = Db::table('s_course')->delete($DeleteCourseData);
        $DeleteTeaching = Db::table('s_teaching')->where('c_id', $DeleteCourseData['id'])->delete();

        //这里之所以不写回滚判断是因为，你不能确定科目被老师选取了，
        //如果没有选取，这表示师科表里面没有c_id ，也会到导致$DeleteTeaching返回一直是0

        //提交
        Db::commit();
        if($DeleteCourse){
            //注意路径,和相关函数的使用
            return $this->success('删除成功','Course/SelectCourse');

            //这是给HTML视图进行渲染的
            // return $this->fetch('/Classes/SelectClasses');
        }else{
            return $this->error('删除失败','Course/SelectCourse');
        }
    }

    public function UpdateCourse(){
        if($this->request->isPost()){

            $UpdateCourseData = $this->request->post();
           // var_dump($UpdateCourseData);die;

            $UpdateCourse = Db::table('s_course')->where('id',$UpdateCourseData['id'])->update($UpdateCourseData);
            if($UpdateCourse){

                return $this->success('修改成功','Course/SelectCourse');
            }else{
                return $this->error('修改失败','Course/SelectCourse');
            }
        }else{

            $SelectGetCourseData = $this->request->get();
            $SelectGetCourse = Db::table('s_course')->get($SelectGetCourseData);
            $this->assign('SelectGetCourse',$SelectGetCourse);

            if($this->request->isAjax()){
                $this->Ajax('','',$SelectGetCourse);
            }

            return $this->fetch('CourseView/UpdateCourse');
        }

    }


    public function UpCourseStatus(){
        $status = $this->request->post();
      //  var_dump($status);die;
        $result = Db::table('s_course')->where('id',$status['id'])->update($status);
    }




    public function Ajax(string $code,string $msg,$dataAjax = [],$count = NULL){
        $ajax = [
            'code'=>$code ?$code:0,
            'msg'=>$msg ? $msg : 'fill',
            'dataAjax'=>$dataAjax,
            "count"=>$count
        ];
        echo (json_encode($ajax));die;
    }




}